package com.woniuxy.util;

import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Service
public class UserContext {
    public static Integer contextUserId() throws Exception {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        String jwt = request.getHeader("Authorization");
        Map<String, Object> userMap = JWTUtil.getUserMap(jwt);
        return ((Long) userMap.get("id")).intValue();
    }
}